(function() {
  var checking_freeze, update_validator;

  checking_freeze = function(form) {
    var in_checking;
    in_checking = form.form('get value', 'in_checking');
    if (in_checking !== 'true') {
      return;
    }
    $('body').css('background-color', 'black');
    return freeze_form(form);
  };

  update_validator = function(form, inside) {
    var fields;
    fields = {
      merid: {
        optional: false,
        rules: [
          {
            type: 'empty'
          }
        ]
      },
      termid: {
        optional: false,
        rules: [
          {
            type: 'empty'
          }, {
            type: 'doesntContain[ ]'
          }
        ]
      }
    };
    if (inside) {
      fields.tmk = {
        optional: false,
        rules: [
          {
            type: 'minLength[32]'
          }, {
            type: 'maxLength[32]'
          }, {
            type: 'regExp[/^[0-9a-fA-F]+$/]'
          }
        ]
      };
      fields.macalgo = {
        optional: false,
        rules: [
          {
            type: 'empty'
          }
        ]
      };
    }
    form.form('destroy');
    form.form({
      inline: true,
      fields: fields
    });
    form.find('.field.error').removeClass('error');
    return form.find('.pointing.prompt.label').remove();
  };

  $(function() {
    var form, record;
    form = $('form.main');
    update_validator(form, false);
    checking_freeze(form);
    record = form.form('get value', 'record');
    if (record && record.trim().length > 0) {
      record = JSON.parse(record);
      form.form('set value', 'record', '');
    } else {
      record = null;
    }
    form.find('.dropdown.merid').dropdown({
      onChange: function(value, text, $choice) {
        var $mapto_term, inside;
        inside = $choice.data('inside');
        update_validator(form, inside);
        form.find('.dropdown.merid').data('inside', inside);
        $mapto_term = form.find('.dropdown.mapto_term');
        if (!inside) {
          return $mapto_term.addClass('disabled').dropdown('clear');
        }
        $mapto_term.addClass('loading');
        return $.post('/terms/query/mapto_terms', {
          chnl: $choice.data('chnl')
        }, function(resp) {
          var color, item, items, _i, _len, _ref;
          $mapto_term.removeClass('loading');
          if (!resp.succ) {
            return;
          }
          items = '<div class="item" data-value=" ">无</div>';
          _ref = resp.records;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            item = _ref[_i];
            inside = item.inside ? '平台商户' : '外联商户';
            items += "<div class='item' data-value='" + item.uuid + "'>";
            items += "" + item.termid;
            color = item.shaded ? 'orange' : '#888';
            items += "<div class='right floated' style='font-size:small; color:" + color + ";'>" + item.mer_name + " ｜ ";
            items += "" + item.mer_no + " ｜ ";
            items += "" + inside + " ｜ ";
            items += "" + item.mer_chnl;
            items += "</div>";
            items += "</div>";
          }
          $mapto_term.find('.menu').html(items);
          $mapto_term.removeClass('disabled').dropdown('refresh');
          if (record && record.mapto_term) {
            form.form('set value', 'mapto_term', record.mapto_term);
          }
          return checking_freeze(form);
        });
      }
    });
    form.find('.icon.eyes').click(function() {
      var $input_real, $input_show;
      $input_real = $(this).siblings('input.real');
      $input_show = $(this).siblings('input.show');
      if ($(this).data('hide')) {
        $input_show.val($input_real.val());
        $input_show.removeAttr('readonly');
        $(this).removeClass('unhide').addClass('hide red');
        return $(this).data('hide', false);
      } else {
        $input_real.val($input_show.val());
        $input_show.val($input_real.val() ? '••••••••' : '');
        $input_show.attr('readonly', 'readonly');
        $(this).removeClass('hide red').addClass('unhide');
        return $(this).data('hide', true);
      }
    });
    form.find('a#tmk_gen').click(function(evt) {
      var i, key, t, tmk, tmk_hide, _i;
      evt.preventDefault();
      key = '';
      for (i = _i = 0; _i <= 31; i = ++_i) {
        t = Math.ceil(Math.random() * 15);
        t = t > 9 ? t - 10 + 65 : t + 48;
        key += String.fromCharCode(t);
      }
      tmk_hide = form.form('get value', 'tmk_hide');
      tmk = form.form('get value', 'tmk');
      if (tmk_hide === tmk) {
        form.form('set value', 'tmk_hide', key);
      }
      return form.form('set value', 'tmk', key);
    });
    form.find('button#input_delete').click(function(evt) {
      if ($(this).hasClass('has_clicked')) {
        $(this).removeClass('has_clicked').text('删 除');
        return $.post('/terms/input/delete', {
          serial: form.form('get value', 'serial'),
          uuid: form.form('get value', 'uuid')
        });
      } else {
        return $(this).addClass('has_clicked').text('确认删除');
      }
    });
    form.find('button#pass').click(function() {
      var effect_time;
      if ($(this).hasClass('has_clicked')) {
        effect_time = form.form('get value', 'effect_time');
        if (!effect_time || effect_time.trim().length === 0) {
          alert('请选择信息生效的时间');
          return;
        }
        $(this).removeClass('has_clicked').text('通 过');
        form.attr('action', '/terms/check/pass');
        return form.submit();
      } else {
        form.find('button#reject').text('拒 绝').removeClass('has_clicked');
        form.find('.fields#reject_reason').hide();
        form.find('.fields#effect_time').show();
        return $(this).addClass('has_clicked').text('确认通过');
      }
    });
    form.find('button#reject').click(function() {
      var reason;
      if ($(this).hasClass('has_clicked')) {
        reason = form.form('get value', 'reject_reason');
        if (!reason || reason.trim().length === 0) {
          alert('请输入拒绝的原因，以便录入人员可以有针对性的进行修改');
          return;
        }
        $(this).removeClass('has_clicked').text('拒 绝');
        form.attr('action', '/terms/check/reject');
        return form.submit();
      } else {
        form.find('button#pass').text('通 过').removeClass('has_clicked');
        form.find('.fields#effect_time').hide();
        form.find('.fields#reject_reason').show();
        return $(this).addClass('has_clicked').text('确认拒绝');
      }
    });
    if (record) {
      form.form('set values', record);
    }
    return form.submit(function(evt) {
      form.find('.icon.eyes').each(function() {
        var $input_real, $input_show;
        if (!$(this).data('hide')) {
          $input_real = $(this).siblings('input.real');
          $input_show = $(this).siblings('input.show');
          return $input_real.val($input_show.val());
        }
      });
      if (!form.form('is valid')) {
        evt.preventDefault();
        form.find('button#ok').removeClass('loading');
        return false;
      }
    });
  });

}).call(this);
